package com.example.service.impl;

import com.example.mapper.BrandMapper;
import com.example.pojo.Brand;
import com.example.pojo.BrandExample;
import com.example.req.BrandReq;
import com.example.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<Brand> queryByCondition(BrandReq brandReq) {
        BrandExample example = new BrandExample();
        BrandExample.Criteria criteria = example.createCriteria();
        if (brandReq != null) {
            if (!ObjectUtils.isEmpty(brandReq.getName())){
                criteria.andNameLike("%" + brandReq.getName() + "%");
            }
            if (!ObjectUtils.isEmpty(brandReq.getFirstChar())){
                criteria.andFirstCharEqualTo(brandReq.getFirstChar());
            }
        }
        return brandMapper.selectByExample(example);
    }

    @Override
    public int saveOrUpdate(Brand brand) {
        if (brand.getId() != null){
            return brandMapper.updateByPrimaryKey(brand);
        }else {
            return brandMapper.insert(brand);
        }
    }

    @Override
    public int deleteById(Long id) {
        return brandMapper.deleteByPrimaryKey(id);
    }
}
